package org.example.rowing.service;

import lombok.extern.slf4j.Slf4j;
import org.example.rowing.domain.RoundScore;
import org.example.rowing.domain.Team;
import org.example.rowing.vo.RaceVO;
import org.example.rowing.vo.RegistrationVO;
import org.example.rowing.vo.RoundScoreVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
@Slf4j
public class ProcessThirtyOneToThirtySixBoatsService implements OverRaceProcessStrategy {

    @Autowired
    private RaceInfoService raceInfoService;
    @Autowired
    private RaceProcessCommonService raceProcessCommonService;

    public static final List<String> teamLanes = Arrays.asList("",
            "A组3号", "A组4号", "A组2号", "A组5号", "A组1号", "A组6号",
            "B组3号", "B组4号", "B组2号", "B组5号", "B组1号", "B组6号",
            "C组3号", "C组4号", "C组2号", "C组5号", "C组1号", "C组6号",
            "D组3号", "D组4号", "D组2号", "D组5号", "D组1号", "D组6号",
            "E组3号", "E组4号", "E组2号", "E组5号", "E组1号", "E组6号",
            "F组3号", "F组4号", "F组2号", "F组5号", "F组1号", "F组6号"
    );

    /**
     * 获取预赛晋级半决赛的列表
     * @param curList
     */
    public void getPreToSemiFinal(List<RoundScoreVO> curList, Long raceId) {}

    @Override
    public void getTimeTrialToPre(List<RoundScoreVO> curList, Long raceId) {

    }

    /**
     * 获取预赛晋级1/4决赛的列表
     * @param curList
     */
    public void getPreToQuarterFinal(List<RoundScoreVO> curList, Long raceId) {
        List<RoundScoreVO> quarterFinalA = new ArrayList<>();
        List<RoundScoreVO> quarterFinalB = new ArrayList<>();
        List<RoundScoreVO> quarterFinalC = new ArrayList<>();
        List<RoundScoreVO> quarterFinalD = new ArrayList<>();
        List<RoundScoreVO> groupAList = new ArrayList<>();
        List<RoundScoreVO> groupBList = new ArrayList<>();
        List<RoundScoreVO> groupCList = new ArrayList<>();
        List<RoundScoreVO> groupDList = new ArrayList<>();
        List<RoundScoreVO> groupEList = new ArrayList<>();
        List<RoundScoreVO> groupFList = new ArrayList<>();

        for (RoundScoreVO vo : curList) {
            String lane = vo.getLane();
            if (lane != null) {
                if (lane.startsWith("A组")) {
                    groupAList.add(vo);
                } else if (lane.startsWith("B组")) {
                    groupBList.add(vo);
                } else if (lane.startsWith("C组")) {
                    groupCList.add(vo);
                } else if (lane.startsWith("D组")) {
                    groupDList.add(vo);
                } else if (lane.startsWith("E组")) {
                    groupEList.add(vo);
                } else if (lane.startsWith("F组")) {
                    groupFList.add(vo);
                }
            }
        }

        raceProcessCommonService.sortedEntries(groupAList);
        raceProcessCommonService.sortedEntries(groupBList);
        raceProcessCommonService.sortedEntries(groupCList);
        raceProcessCommonService.sortedEntries(groupDList);
        raceProcessCommonService.sortedEntries(groupEList);
        raceProcessCommonService.sortedEntries(groupFList);

        allocateByRank(groupAList, quarterFinalA, quarterFinalC, quarterFinalD, 1, 3);
        allocateByRank(groupBList, quarterFinalA, quarterFinalD, quarterFinalC, 1, 3);
        allocateByRank(groupCList, quarterFinalB, quarterFinalA, quarterFinalC, 1, 3);
        allocateByRank(groupDList, quarterFinalB, quarterFinalD, quarterFinalB, 1, 3);
        allocateByRank(groupEList, quarterFinalC, quarterFinalB, quarterFinalD, 1, 3);
        allocateByRank(groupFList, quarterFinalD, quarterFinalC, quarterFinalA, 1, 3);


        raceProcessCommonService.sortedEntries(quarterFinalA);
        raceProcessCommonService.sortedEntries(quarterFinalB);
        raceProcessCommonService.sortedEntries(quarterFinalC);
        raceProcessCommonService.sortedEntries(quarterFinalD);

        for (int i = 0; i < quarterFinalA.size(); i++) {
            RoundScoreVO vo = quarterFinalA.get(i);
            RegistrationVO registrationVO = vo.getRegistrationVO();
            // 保存用户输入的分数
            RoundScoreVO roundScore = new RoundScoreVO(raceId, registrationVO, "1/4决赛", null, teamLanes.get(i + 1), null, vo.getStatus());
            raceInfoService.saveRoundScore(roundScore);
        }

        for (int i = 0; i < quarterFinalB.size(); i++) {
            RoundScoreVO vo = quarterFinalB.get(i);
            RegistrationVO registrationVO = vo.getRegistrationVO();
            // 保存用户输入的分数
            RoundScoreVO roundScore = new RoundScoreVO(raceId, registrationVO, "1/4决赛", null, teamLanes.get(i + 7), null, vo.getStatus());
            raceInfoService.saveRoundScore(roundScore);
        }

        for (int i = 0; i < quarterFinalC.size(); i++) {
            RoundScoreVO vo = quarterFinalC.get(i);
            RegistrationVO registrationVO = vo.getRegistrationVO();
            // 保存用户输入的分数
            RoundScoreVO roundScore = new RoundScoreVO(raceId, registrationVO, "1/4决赛", null, teamLanes.get(i + 13), null, vo.getStatus());
            raceInfoService.saveRoundScore(roundScore);
        }

        for (int i = 0; i < quarterFinalD.size(); i++) {
            RoundScoreVO vo = quarterFinalD.get(i);
            RegistrationVO registrationVO = vo.getRegistrationVO();
            // 保存用户输入的分数
            RoundScoreVO roundScore = new RoundScoreVO(raceId, registrationVO, "1/4决赛", null, teamLanes.get(i + 19), null, vo.getStatus());
            raceInfoService.saveRoundScore(roundScore);
        }
    }

    /**
     * 获取预赛晋级复赛的列表
     * @param curList
     * @param raceId
     */
    public void getPreToIntermediary(List<RoundScoreVO> curList, Long raceId) {
        List<RoundScoreVO> intermediaryA = new ArrayList<>();
        List<RoundScoreVO> intermediaryB = new ArrayList<>();
        List<RoundScoreVO> intermediaryC = new ArrayList<>();
        List<RoundScoreVO> groupAList = new ArrayList<>();
        List<RoundScoreVO> groupBList = new ArrayList<>();
        List<RoundScoreVO> groupCList = new ArrayList<>();
        List<RoundScoreVO> groupDList = new ArrayList<>();
        List<RoundScoreVO> groupEList = new ArrayList<>();
        List<RoundScoreVO> groupFList = new ArrayList<>();

        for (RoundScoreVO vo : curList) {
            String lane = vo.getLane();
            if (lane != null) {
                if (lane.startsWith("A组")) {
                    groupAList.add(vo);
                } else if (lane.startsWith("B组")) {
                    groupBList.add(vo);
                } else if (lane.startsWith("C组")) {
                    groupCList.add(vo);
                } else if (lane.startsWith("D组")) {
                    groupDList.add(vo);
                } else if (lane.startsWith("E组")) {
                    groupEList.add(vo);
                } else if (lane.startsWith("F组")) {
                    groupFList.add(vo);
                }
            }
        }

        raceProcessCommonService.sortedEntries(groupAList);
        raceProcessCommonService.sortedEntries(groupBList);
        raceProcessCommonService.sortedEntries(groupCList);
        raceProcessCommonService.sortedEntries(groupDList);
        raceProcessCommonService.sortedEntries(groupEList);
        raceProcessCommonService.sortedEntries(groupFList);

        allocateByRank(groupAList, intermediaryB, intermediaryC, intermediaryA, 4, 6);
        allocateByRank(groupBList, intermediaryA, intermediaryB, intermediaryC, 4, 6);
        allocateByRank(groupCList, intermediaryC, intermediaryA, intermediaryB, 4, 6);
        allocateByRank(groupDList, intermediaryB, intermediaryC, intermediaryA, 4, 6);
        allocateByRank(groupEList, intermediaryA, intermediaryB, intermediaryC, 4, 6);
        allocateByRank(groupFList, intermediaryC, intermediaryA, intermediaryB, 4, 6);

        raceProcessCommonService.sortedEntries(intermediaryA);
        raceProcessCommonService.sortedEntries(intermediaryB);
        raceProcessCommonService.sortedEntries(intermediaryC);

        for (int i = 0; i < intermediaryA.size(); i++) {
            RoundScoreVO vo = intermediaryA.get(i);
            RegistrationVO registrationVO = vo.getRegistrationVO();
            // 保存用户输入的分数
            RoundScoreVO roundScore = new RoundScoreVO(raceId, registrationVO, "复赛", null, teamLanes.get(i + 1), null, vo.getStatus());
            raceInfoService.saveRoundScore(roundScore);
        }

        for (int i = 0; i < intermediaryB.size(); i++) {
            RoundScoreVO vo = intermediaryB.get(i);
            RegistrationVO registrationVO = vo.getRegistrationVO();
            // 保存用户输入的分数
            RoundScoreVO roundScore = new RoundScoreVO(raceId, registrationVO, "复赛", null, teamLanes.get(i + 7), null, vo.getStatus());
            raceInfoService.saveRoundScore(roundScore);
        }

        for (int i = 0; i < intermediaryC.size(); i++) {
            RoundScoreVO vo = intermediaryC.get(i);
            RegistrationVO registrationVO = vo.getRegistrationVO();
            // 保存用户输入的分数
            RoundScoreVO roundScore = new RoundScoreVO(raceId, registrationVO, "复赛", null, teamLanes.get(i + 13), null, vo.getStatus());
            raceInfoService.saveRoundScore(roundScore);
        }
    }

    /**
     * 获取复赛晋级半决赛的列表
     * @param curList
     * @param raceId
     */
    public void getInterToSemiFinal(List<RoundScoreVO> curList, Long raceId) {
        List<RoundScoreVO> semiFinalE = new ArrayList<>();
        List<RoundScoreVO> semiFinalF = new ArrayList<>();
        List<RoundScoreVO> groupAList = new ArrayList<>();
        List<RoundScoreVO> groupBList = new ArrayList<>();
        List<RoundScoreVO> groupCList = new ArrayList<>();

        for (RoundScoreVO vo : curList) {
            String lane = vo.getLane();
            if (lane != null) {
                if (lane.startsWith("A组")) {
                    groupAList.add(vo);
                } else if (lane.startsWith("B组")) {
                    groupBList.add(vo);
                } else if (lane.startsWith("C组")) {
                    groupCList.add(vo);
                }
            }
        }

        raceProcessCommonService.sortedEntries(groupAList);
        raceProcessCommonService.sortedEntries(groupBList);
        raceProcessCommonService.sortedEntries(groupCList);

        allocateByRank(groupAList, semiFinalE, semiFinalF, 3, 6);
        allocateByRank(groupBList, semiFinalF, semiFinalE, 3, 6);
        allocateByRank(groupCList, semiFinalE, semiFinalF, 3, 6);

        for (int i = 0; i < semiFinalE.size(); i++) {
            RoundScoreVO vo = semiFinalE.get(i);
            RegistrationVO registrationVO = vo.getRegistrationVO();
            // 保存用户输入的分数
            RoundScoreVO roundScore = new RoundScoreVO(raceId, registrationVO, "半决赛", null, teamLanes.get(i + 25), null, vo.getStatus());
            raceInfoService.saveRoundScore(roundScore);
        }

        for (int i = 0; i < semiFinalF.size(); i++) {
            RoundScoreVO vo = semiFinalF.get(i);
            RegistrationVO registrationVO = vo.getRegistrationVO();
            // 保存用户输入的分数
            RoundScoreVO roundScore = new RoundScoreVO(raceId, registrationVO, "半决赛", null, teamLanes.get(i + 31), null, vo.getStatus());
            raceInfoService.saveRoundScore(roundScore);
        }
    }

    /**
     * 获取复赛晋级1/4决赛的列表
     * @param curList
     * @param raceId
     */
    public void getInterToQuarterFinal(List<RoundScoreVO> curList, Long raceId) {
        List<RoundScoreVO> quarterFinalA = new ArrayList<>();
        List<RoundScoreVO> quarterFinalB = new ArrayList<>();
        List<RoundScoreVO> quarterFinalC = new ArrayList<>();
        List<RoundScoreVO> quarterFinalD = new ArrayList<>();
        List<RoundScoreVO> groupAList = new ArrayList<>();
        List<RoundScoreVO> groupBList = new ArrayList<>();
        List<RoundScoreVO> groupCList = new ArrayList<>();

        for (RoundScoreVO vo : curList) {
            String lane = vo.getLane();
            if (lane != null) {
                if (lane.startsWith("A组")) {
                    groupAList.add(vo);
                } else if (lane.startsWith("B组")) {
                    groupBList.add(vo);
                } else if (lane.startsWith("C组")) {
                    groupCList.add(vo);
                }
            }
        }

        raceProcessCommonService.sortedEntries(groupAList);
        raceProcessCommonService.sortedEntries(groupBList);
        raceProcessCommonService.sortedEntries(groupCList);

        quarterFinalA.add(groupAList.get(0));
        quarterFinalA.add(groupCList.get(1));
        quarterFinalB.add(groupCList.get(0));
        quarterFinalB.add(groupBList.get(1));
        quarterFinalC.add(groupBList.get(0));
        quarterFinalD.add(groupAList.get(1));

        for (int i = 0; i < quarterFinalA.size(); i++) {
            RoundScoreVO vo = quarterFinalA.get(i);
            RegistrationVO registrationVO = vo.getRegistrationVO();
            // 保存用户输入的分数
            RoundScoreVO roundScore = new RoundScoreVO(raceId, registrationVO, "1/4决赛", null, teamLanes.get(i + 5), null, vo.getStatus());
            raceInfoService.saveRoundScore(roundScore);
        }

        for (int i = 0; i < quarterFinalB.size(); i++) {
            RoundScoreVO vo = quarterFinalB.get(i);
            RegistrationVO registrationVO = vo.getRegistrationVO();
            // 保存用户输入的分数
            RoundScoreVO roundScore = new RoundScoreVO(raceId, registrationVO, "1/4决赛", null, teamLanes.get(i + 11), null, vo.getStatus());
            raceInfoService.saveRoundScore(roundScore);
        }

        for (int i = 0; i < quarterFinalC.size(); i++) {
            RoundScoreVO vo = quarterFinalC.get(i);
            RegistrationVO registrationVO = vo.getRegistrationVO();
            // 保存用户输入的分数
            RoundScoreVO roundScore = new RoundScoreVO(raceId, registrationVO, "1/4决赛", null, teamLanes.get(i + 18), null, vo.getStatus());
            raceInfoService.saveRoundScore(roundScore);
        }

        for (int i = 0; i < quarterFinalD.size(); i++) {
            RoundScoreVO vo = quarterFinalD.get(i);
            RegistrationVO registrationVO = vo.getRegistrationVO();
            // 保存用户输入的分数
            RoundScoreVO roundScore = new RoundScoreVO(raceId, registrationVO, "1/4决赛", null, teamLanes.get(i + 24), null, vo.getStatus());
            raceInfoService.saveRoundScore(roundScore);
        }
    }

    /**
     * 获取复赛晋级决赛的列表
     * @param curList
     * @param raceId
     */
    public void getInterToFinal(List<RoundScoreVO> curList, Long raceId) {}

    /**
     * 获取1/4决赛晋级半决赛的列表
     * @param curList
     * @param raceId
     */
    public void getQuarterToSemiFinal(List<RoundScoreVO> curList, Long raceId) {
        List<RoundScoreVO> semiFinalA = new ArrayList<>();
        List<RoundScoreVO> semiFinalB = new ArrayList<>();
        List<RoundScoreVO> semiFinalC = new ArrayList<>();
        List<RoundScoreVO> semiFinalD = new ArrayList<>();
        List<RoundScoreVO> groupAList = new ArrayList<>();
        List<RoundScoreVO> groupBList = new ArrayList<>();
        List<RoundScoreVO> groupCList = new ArrayList<>();
        List<RoundScoreVO> groupDList = new ArrayList<>();

        for (RoundScoreVO vo : curList) {
            String lane = vo.getLane();
            if (lane != null) {
                if (lane.startsWith("A组")) {
                    groupAList.add(vo);
                } else if (lane.startsWith("B组")) {
                    groupBList.add(vo);
                } else if (lane.startsWith("C组")) {
                    groupCList.add(vo);
                } else if (lane.startsWith("D组")) {
                    groupDList.add(vo);
                }
            }
        }

        raceProcessCommonService.sortedEntries(groupAList);
        raceProcessCommonService.sortedEntries(groupBList);
        raceProcessCommonService.sortedEntries(groupCList);
        raceProcessCommonService.sortedEntries(groupDList);

        allocateByRank(groupAList, semiFinalA, semiFinalB, 1, 3);
        allocateByRank(groupBList, semiFinalA, semiFinalB, 1, 3);
        allocateByRank(groupCList, semiFinalB, semiFinalA, 1, 3);
        allocateByRank(groupDList, semiFinalB, semiFinalA, 1, 3);

        allocateByRank(groupAList, semiFinalC, semiFinalD, 4, 6);
        allocateByRank(groupBList, semiFinalC, semiFinalD, 4, 6);
        allocateByRank(groupCList, semiFinalD, semiFinalC, 4, 6);
        allocateByRank(groupDList, semiFinalD, semiFinalC, 4, 6);

        raceProcessCommonService.sortedEntries(semiFinalA);
        raceProcessCommonService.sortedEntries(semiFinalB);
        raceProcessCommonService.sortedEntries(semiFinalC);
        raceProcessCommonService.sortedEntries(semiFinalD);

        for (int i = 0; i < semiFinalA.size(); i++) {
            RoundScoreVO vo = semiFinalA.get(i);
            RegistrationVO registrationVO = vo.getRegistrationVO();
            // 保存用户输入的分数
            RoundScoreVO roundScore = new RoundScoreVO(raceId, registrationVO, "半决赛", null, teamLanes.get(i + 1), null, vo.getStatus());
            raceInfoService.saveRoundScore(roundScore);
        }

        for (int i = 0; i < semiFinalB.size(); i++) {
            RoundScoreVO vo = semiFinalB.get(i);
            RegistrationVO registrationVO = vo.getRegistrationVO();
            // 保存用户输入的分数
            RoundScoreVO roundScore = new RoundScoreVO(raceId, registrationVO, "半决赛", null, teamLanes.get(i + 7), null, vo.getStatus());
            raceInfoService.saveRoundScore(roundScore);
        }

        for (int i = 0; i < semiFinalC.size(); i++) {
            RoundScoreVO vo = semiFinalC.get(i);
            RegistrationVO registrationVO = vo.getRegistrationVO();
            // 保存用户输入的分数
            RoundScoreVO roundScore = new RoundScoreVO(raceId, registrationVO, "半决赛", null, teamLanes.get(i + 13), null, vo.getStatus());
            raceInfoService.saveRoundScore(roundScore);
        }

        for (int i = 0; i < semiFinalD.size(); i++) {
            RoundScoreVO vo = semiFinalD.get(i);
            RegistrationVO registrationVO = vo.getRegistrationVO();
            // 保存用户输入的分数
            RoundScoreVO roundScore = new RoundScoreVO(raceId, registrationVO, "半决赛", null, teamLanes.get(i + 19), null, vo.getStatus());
            raceInfoService.saveRoundScore(roundScore);
        }
    }

    /**
     * 获取半决赛晋级决赛的列表
     * @param curList
     * @param raceId
     */
    public void getSemiToFinal(List<RoundScoreVO> curList, Long raceId) {
        List<RoundScoreVO> finalA = new ArrayList<>();
        List<RoundScoreVO> finalB = new ArrayList<>();
        List<RoundScoreVO> finalC = new ArrayList<>();
        List<RoundScoreVO> finalD = new ArrayList<>();
        List<RoundScoreVO> finalE = new ArrayList<>();
        List<RoundScoreVO> finalF = new ArrayList<>();
        List<RoundScoreVO> groupAList = new ArrayList<>();
        List<RoundScoreVO> groupBList = new ArrayList<>();
        List<RoundScoreVO> groupCList = new ArrayList<>();
        List<RoundScoreVO> groupDList = new ArrayList<>();
        List<RoundScoreVO> groupEList = new ArrayList<>();
        List<RoundScoreVO> groupFList = new ArrayList<>();

        for (RoundScoreVO vo : curList) {
            String lane = vo.getLane();
            if (lane != null) {
                if (lane.startsWith("A组")) {
                    groupAList.add(vo);
                } else if (lane.startsWith("B组")) {
                    groupBList.add(vo);
                } else if (lane.startsWith("C组")) {
                    groupCList.add(vo);
                } else if (lane.startsWith("D组")) {
                    groupDList.add(vo);
                } else if (lane.startsWith("E组")) {
                    groupEList.add(vo);
                } else if (lane.startsWith("F组")) {
                    groupFList.add(vo);
                }
            }
        }

        raceProcessCommonService.sortedEntries(groupAList);
        raceProcessCommonService.sortedEntries(groupBList);
        raceProcessCommonService.sortedEntries(groupCList);
        raceProcessCommonService.sortedEntries(groupDList);
        raceProcessCommonService.sortedEntries(groupEList);
        raceProcessCommonService.sortedEntries(groupFList);

        for (int i = 0; i < 3; i++) {
            if (i % 2 == 0) {
                finalA.add(groupAList.get(i));
                finalA.add(groupBList.get(i));
            } else {
                finalA.add(groupBList.get(i));
                finalA.add(groupAList.get(i));
            }
        }
        for (int i = 3; i < 6; i++) {
            if (i % 2 == 0) {
                finalB.add(groupAList.get(i));
                finalB.add(groupBList.get(i));
            } else {
                finalB.add(groupBList.get(i));
                finalB.add(groupAList.get(i));
            }
        }
        for (int i = 0; i < 3; i++) {
            if (i % 2 == 0) {
                finalC.add(groupCList.get(i));
                finalC.add(groupDList.get(i));
            } else {
                finalC.add(groupDList.get(i));
                finalC.add(groupCList.get(i));
            }
        }
        for (int i = 3; i < 6; i++) {
            if (i % 2 == 0) {
                finalD.add(groupCList.get(i));
                finalD.add(groupDList.get(i));
            } else {
                finalD.add(groupDList.get(i));
                finalD.add(groupCList.get(i));
            }
        }
        if (groupEList.size() + groupFList.size() <= 7) {
            int min = Math.min(groupEList.size() - 1, groupFList.size() - 1);
            for (int i = 0; i < min; i++) {
                if (i % 2 == 0) {
                    finalE.add(groupEList.get(i));
                    finalE.add(groupFList.get(i));
                } else {
                    finalE.add(groupFList.get(i));
                    finalE.add(groupEList.get(i));
                }
            }
            for (int i = min; i < groupEList.size() - 1; i++) {
                finalE.add(groupEList.get(i));
            }
            for (int i = min; i < groupFList.size() - 1; i++) {
                finalE.add(groupFList.get(i));
            }
            finalF.add(groupEList.get(groupEList.size() - 1));
            finalF.add(groupFList.get(groupFList.size() - 1));
        } else {
            for (int i = 0; i < 3; i++) {
                if (i % 2 == 0) {
                    finalE.add(groupEList.get(i));
                    finalE.add(groupFList.get(i));
                } else {
                    finalE.add(groupFList.get(i));
                    finalE.add(groupEList.get(i));
                }
            }
            int min = Math.min(groupEList.size(), groupFList.size());
            for (int i = 3; i < min; i++) {
                if (i % 2 == 0) {
                    finalF.add(groupEList.get(i));
                    finalF.add(groupFList.get(i));
                } else {
                    finalF.add(groupFList.get(i));
                    finalF.add(groupEList.get(i));
                }
            }
            for (int i = min; i < groupEList.size(); i++) {
                finalF.add(groupEList.get(i));
            }
            for (int i = min; i < groupFList.size(); i++) {
                finalF.add(groupFList.get(i));
            }
        }

        for (int i = 0; i < finalA.size(); i++) {
            RoundScoreVO vo = finalA.get(i);
            RegistrationVO registrationVO = vo.getRegistrationVO();
            // 保存用户输入的分数
            RoundScoreVO roundScore = new RoundScoreVO(raceId, registrationVO, "决赛", null, teamLanes.get(i + 1), null, vo.getStatus());
            raceInfoService.saveRoundScore(roundScore);
        }

        for (int i = 0; i < finalB.size(); i++) {
            RoundScoreVO vo = finalB.get(i);
            RegistrationVO registrationVO = vo.getRegistrationVO();
            // 保存用户输入的分数
            RoundScoreVO roundScore = new RoundScoreVO(raceId, registrationVO, "决赛", null, teamLanes.get(i + 7), null, vo.getStatus());
            raceInfoService.saveRoundScore(roundScore);
        }

        for (int i = 0; i < finalC.size(); i++) {
            RoundScoreVO vo = finalC.get(i);
            RegistrationVO registrationVO = vo.getRegistrationVO();
            // 保存用户输入的分数
            RoundScoreVO roundScore = new RoundScoreVO(raceId, registrationVO, "决赛", null, teamLanes.get(i + 13), null, vo.getStatus());
            raceInfoService.saveRoundScore(roundScore);
        }

        for (int i = 0; i < finalD.size(); i++) {
            RoundScoreVO vo = finalD.get(i);
            RegistrationVO registrationVO = vo.getRegistrationVO();
            // 保存用户输入的分数
            RoundScoreVO roundScore = new RoundScoreVO(raceId, registrationVO, "决赛", null, teamLanes.get(i + 19), null, vo.getStatus());
            raceInfoService.saveRoundScore(roundScore);
        }

        for (int i = 0; i < finalE.size(); i++) {
            RoundScoreVO vo = finalE.get(i);
            RegistrationVO registrationVO = vo.getRegistrationVO();
            // 保存用户输入的分数
            RoundScoreVO roundScore = new RoundScoreVO(raceId, registrationVO, "决赛", null, teamLanes.get(i + 25), null, vo.getStatus());
            raceInfoService.saveRoundScore(roundScore);
        }

        for (int i = 0; i < finalF.size(); i++) {
            RoundScoreVO vo = finalF.get(i);
            RegistrationVO registrationVO = vo.getRegistrationVO();
            // 保存用户输入的分数
            RoundScoreVO roundScore = new RoundScoreVO(raceId, registrationVO, "决赛", null, teamLanes.get(i + 31), null, vo.getStatus());
            raceInfoService.saveRoundScore(roundScore);
        }
    }

    /**
     * 获取最终排名
     * @param curList
     * @param raceVO
     * @return
     */
    public List<RoundScoreVO> getRank(List<RoundScoreVO> curList, RaceVO raceVO) {
        List<RoundScoreVO> groupAList = new ArrayList<>();
        List<RoundScoreVO> groupBList = new ArrayList<>();
        List<RoundScoreVO> groupCList = new ArrayList<>();
        List<RoundScoreVO> groupDList = new ArrayList<>();
        List<RoundScoreVO> groupEList = new ArrayList<>();
        List<RoundScoreVO> groupFList = new ArrayList<>();

        for (RoundScoreVO vo : curList) {
            String lane = vo.getLane();
            if (lane != null) {
                if (lane.startsWith("A组")) {
                    groupAList.add(vo);
                } else if (lane.startsWith("B组")) {
                    groupBList.add(vo);
                } else if (lane.startsWith("C组")) {
                    groupCList.add(vo);
                } else if (lane.startsWith("D组")) {
                    groupDList.add(vo);
                } else if (lane.startsWith("E组")) {
                    groupEList.add(vo);
                } else if (lane.startsWith("F组")) {
                    groupFList.add(vo);
                }
            }
        }

        raceProcessCommonService.sortedEntries(groupAList);
        raceProcessCommonService.sortedEntries(groupBList);
        raceProcessCommonService.sortedEntries(groupCList);
        raceProcessCommonService.sortedEntries(groupDList);
        raceProcessCommonService.sortedEntries(groupEList);
        raceProcessCommonService.sortedEntries(groupFList);

        groupAList.addAll(groupBList);
        groupAList.addAll(groupCList);
        groupAList.addAll(groupDList);
        groupAList.addAll(groupEList);
        groupAList.addAll(groupFList);

        saveFinalResult(groupAList, raceVO);

        int i = 0;
        for (RoundScoreVO roundScoreVO : groupAList) {
            Double v = Double.valueOf(i++);
            RoundScore roundScore = new RoundScore(raceVO.getId(), "比赛结果", null, null, v, roundScoreVO.getRegistrationVO().getRegistrationId(), roundScoreVO.getStatus());
            raceInfoService.saveRoundScore(roundScore);
        }
        return groupAList;
    }


    /**
     *
     * @param group
     * @param halfA
     * @param halfB
     * @param start 起始排名（从1开始）
     * @param end   结束排名
     */
    private void allocateByRank(List<RoundScoreVO> group, List<RoundScoreVO> halfA, List<RoundScoreVO> halfB, int start, int end) {
        int startIndex = start - 1; // 第1名（索引0）
        int endIndex = Math.min(group.size(), end); // 最多取到第5名（索引5）

        for (int i = startIndex; i < endIndex; i++) {
            if (i % 2 == 0) {
                halfA.add(group.get(i)); // 2 4
            } else {
                halfB.add(group.get(i)); // 3
            }
        }
    }

    private void allocateByRank(List<RoundScoreVO> group, List<RoundScoreVO> halfA, List<RoundScoreVO> halfB, List<RoundScoreVO> halfC, int start, int end) {
        int startIndex = start - 1; // 第1名（索引0）
        int endIndex = Math.min(group.size(), end); // 最多取到第5名（索引5）

        for (int i = startIndex; i < endIndex; i++) {
            if (i % 3 == 0) {
                halfA.add(group.get(i)); // 0
            } else if (i % 3 == 1) {
                halfB.add(group.get(i)); // 1
            } else if (i % 3 == 2) {
                halfC.add(group.get(i)); // 2
            }
        }
    }

    private void allocateByRank(List<RoundScoreVO> group, List<RoundScoreVO> halfB, List<RoundScoreVO> halfC, List<RoundScoreVO> halfD, List<RoundScoreVO> halfA, int start, int end) {
        int startIndex = start - 1; // 第1名（索引0）
        int endIndex = Math.min(group.size(), end); // 最多取到第5名（索引5）

        for (int i = startIndex; i < endIndex; i++) {
            if (i % 4 == 0) {
                halfA.add(group.get(i)); // 4
            } else if (i % 4 == 1) {
                halfB.add(group.get(i)); // 1 5
            } else if (i % 4 == 2) {
                halfC.add(group.get(i)); // 2
            } else if (i % 4 == 3) {
                halfD.add(group.get(i)); // 3
            }
        }
    }

    private void saveFinalResult(List<RoundScoreVO> finalList, RaceVO raceVO) {
        raceProcessCommonService.saveFinalResult(finalList, raceVO);
    }
}
